<?php 
/**
 *梭哈静态类
 */
class App_showhand{
	/**
	 * 获取梭哈的收入榜
	 * @param <int> $uin
	 * @param <boolean> $insert 是否在不存在时，新写入数据
	 */
	public static function getGain($uin , $insert = true) {
		$Db = loadDB();
		$result = $Db->getOne("SELECT * FROM `app_showhand_gain` WHERE `uin` = " . $uin);
		
		// 如果梭哈数据不存在，则新写入一条数据
		if($insert && $result === false) {
			$query_data = array(
				'uin' => $uin,
				'gain' => 0,
				'total' => 0,
				'win' => 0,
				'rate' => 0,
				'time' => 0,
				'setshowhand' => 0,
			);
			$Db->doInsert('app_showhand_gain' , $query_data);
			
			$result = $query_data;
		}
		
		return $result;
	}
	/**
	 * 获取单个用户称号
	 * Enter description here ...
	 */
	public static function getRank($uin , $gain = false) {
		// 如果没有传进来数据，则从数据库获取
		if($gain === false) {
			$result = self::getGain($uin);
			
			if(empty($result)) $gain = 0;
			else $gain = $result['gain'];
		}
		
	if($gain >= 2000000) {
			return array('level' => 18 , 'name' => '赌圣');
		} else if($gain >= 500000) {
			return array('level' => 17 , 'name' => '全球巨头');
		} else if($gain >= 300000) {
			return array('level' => 16 , 'name' => '亚洲富豪');
		} else if($gain >= 130000) {
			return array('level' => 15 ,  'name' => '内地富豪');
		} else if($gain >= 80000) {
			return array('level' => 14 ,  'name' => '地方豪强');
		} else if($gain >= 50000) {
			return array('level' => 13 ,  'name' => '大财主');
		} else if($gain >= 38000) {
			return array('level' => 12 ,  'name' => '大地主');
		} else if($gain >= 15000) {
			return array('level' => 11 ,  'name' => '土财主');
		} else if($gain >= 8000) {
			return array('level' => 10 ,  'name' => '地主');
		} else if($gain >= 3200) {
			return array('level' => 9 ,  'name' => '富农');
		} else if($gain >= 1600) {
			return array('level' => 8 ,  'name' => '中农');
		} else if($gain >= 920) {
			return array('level' => 7 ,  'name' => '贫农');
		} else if($gain >= 560) {
			return array('level' => 6 ,  'name' => '长工');
		} else if($gain >= 320) {
			return array('level' => 5 ,  'name' => '短工');
		} else if($gain >= 160) {
			return array('level' => 4 ,  'name' => '包身工');
		} else if($gain >= 60) {
			return array('level' => 3 ,  'name' => '佃农');
		} else if($gain >= 0) {
			return array('level' => 2 ,  'name' => '赤脚');
		} else {
			return array('level' => 1 ,  'name' => '农奴');
		}
	}

	/**
	 * 获取多个用户称号
	 * @param <array> $uins
	 * @param <array> $gains
	 */
	public static function getRanks($uins , $gains = false) {
		if(empty($gains)) {
			$uins_str = implode(',' , $uins);
			
			$Db = loadDB();
			$result = $Db->getAll("SELECT `uin` , `gain` FROM `app_showhand_gain` WHERE `uin` IN '{$uins_str}'");
			
			$gains = array();
			if(!empty($result)) {
				foreach($result as $row) {
					$gains[$row['uin']] = $row['gain'];
				}
			}
		}
		
		$return_arr = array();
		foreach($uins as $uin) {
			$gain = empty($gains[$uin]) ? 0 : $gains[$uin];
			$return_arr[$uin] = self::getRank($uin , $gain);
		}
		
		return $return_arr;
	}
	/**
	 * 获取用户已可应战的梭哈牌局
	 * @param <int> $limit
	 */
	public static function getRandStateList() {
		$Db = loadDB();
		
		//$pageSize=($pageSize-1)*$pageCount;
		$result = $Db->getAll("SELECT * FROM `app_showhand_state`,`app_showhand_gain` WHERE `turn` = 1  AND app_showhand_state.uin=app_showhand_gain.uin ORDER BY RAND() DESC LIMIT 10");
		if(empty($result)) return array();
		
		foreach($result as $k => $row) {
			$result[$k]['state'] = unserialize($row['state']);
		}
		
		return $result;
	}
	/**
	 * 获取用户未完成的梭哈牌局
	 * @param <int> $limit
	 */
	public static function getRandUndoneList($uin) {
		$Db = loadDB();
		
		//$pageSize=($pageSize-1)*$pageCount;
		$result = $Db->getAll("SELECT * FROM `app_showhand_state`,`app_showhand_gain` WHERE `turn` = 2  AND app_showhand_state.uin = app_showhand_gain.uin AND app_showhand_state.guest_uin = {$uin} ORDER BY RAND() DESC LIMIT 5");
		if(empty($result)) return array();
		
		foreach($result as $k => $row) {
			$result[$k]['state'] = unserialize($row['state']);
		}
		
		return $result;
	}
	/**
	 * 随机发牌、随机牌的花色
	 * @param <int> $num 随机张数、随机花色样式
	 * 方片(1),梅花(2),红桃(3),黑桃(4)
	 */
	public static function get_rand() {
	     $a = rand(1,4);
	     $b = rand(2,14);
	     return array('a' => $a , 'b' => $b);
	}
	/**
	 * 对比数组是否相同
	 * @param $card
	 * @param $cards
	 */
	public static function check_same($card , $cards) {
	     foreach($cards as $row) {
	          if($row === $card) return true;
	     }
	    
	     return false;
	}
	/**
	 * 按牌花色排列
	 */
	public static function numcard($num){
		$new_arr = array();

		$max = count($num);
		for($i = 0 ; $i < $max ; $i++) {
			$max_data = array(); 
			$max_k = 0;
			foreach($num as $k => $row) {
			// init
				if(empty($max_data)) {
					$max_data = $row;
					$max_k = $k;
					continue;
				}
			// compare
				if($row['b'] > $max_data['b']) {
					$max_data = $row;
					$max_k = $k;
				} else if($row['b'] == $max_data['b'] && $row['a'] > $max_data['a']) {
					$max_data = $row;
					$max_k = $k;
				}
			}
			$new_arr[] = $max_data;
			unset($num[$max_k]);
		} 
		//print_r($new_arr);
		return $new_arr;
	}

	/**
	 * 判断牌型
	 * 同花顺(9)＞四条(8)＞富尔豪斯(7)＞同花(6)＞顺子(5)＞三条(4)＞二对(3)＞单对(2)＞散牌(1)
	 * */
	public static function getfairly($num){
	
		$count_num = count($num);
		
		//判断同花顺
		$is_tierce = true;	
		if($num[0]['b'] === 14 && $num[1]['b'] === 5){
			for($g = 2 ; $g < $count_num ; $g++){
					$k = $g - 1;
					if($num[$k]['b'] - $num[$g]['b'] != 1){
						$is_tierce = false;
					}
					if($num[$g]['a'] != $num[$k]['a']){
						$is_tierce = false;
					}
			}
		}else{
			for($i = 1 ; $i < $count_num ; $i++){
				$j = $i - 1;
				if($num[$j]['b'] - $num[$i]['b'] != 1){
					$is_tierce = false;
				}
				if($num[$i]['a'] !== $num[$j]['a']){
					$is_tierce = false;
				}
			}
		}		
		if(!empty($is_tierce)){
			$array = array($num[0]['a'] , $num[0]['b'] , 9);
			return $array;
		}
		
		//判断同花
		$is_straight = true;	
		for($i = 1 ; $i < $count_num ; $i++){
			$j = $i - 1;
			if($num[$i]['a'] !== $num[$j]['a']){
				$is_straight = false;
			}
		}
		if(!empty($is_straight)){
			$array = array($num[0]['a'] , $num[0]['b'] , 6);
			return $array;
		}
		
		//判断顺子
		$is_tierces = true;
		if($num[0]['b'] === 14 && $num[1]['b'] === 5){
			for($g = 2 ; $g < $count_num ; $g++){
				$k = $g - 1;
				if($num[$k]['b'] - $num[$g]['b'] != 1){
					$is_tierces = false;
				}
		
			}
		}else{
			for($i = 1 ; $i < $count_num ; $i++){
				$j = $i - 1;
				if($num[$j]['b'] - $num[$i]['b'] != 1){
					$is_tierces = false;
				}
			}
		}
		if(!empty($is_tierces)){
			$array = array($num[0]['a'] , $num[0]['b'] , 5);
			return $array;
		}
		//$nu=array(6,5,5,5,5);
		//判断四条
		if($num[0]['b'] === $num[3]['b'] || $num[1]['b'] === $num[4]['b'] ){
			$array = array($num[3]['a'] , $num[3]['b'] , 8);
			return $array;
		}
		//判断富尔豪斯(3条带一对)
		if($num[0]['b'] === $num[2]['b'] && $num[3]['b'] === $num[4]['b']){
			$array = array($num[1]['a'] , array($num[1]['b'] , $num[3]['b']),7);
			return $array;
		}
		if($num[0]['b'] === $num[1]['b'] && $num[2]['b'] === $num[4]['b']){
			$array = array($num[1]['a'] , array($num[3]['b'],$num[1]['b']),7);
			return $array;
		}
		//判断三条
		if($num[0]['b'] === $num[2]['b'] || $num[1]['b'] === $num[3]['b'] || $num[2]['b'] === $num[4]['b']){
			$array=array($num[2]['a'] , $num[2]['b'],4);
			return $array;
		}
		//判断两对
		if($num[0]['b'] === $num[1]['b'] && $num[2]['b'] === $num[3]['b']){
			$array = array($num[1]['a'] , array($num[1]['b'],$num[3]['b']),3);
			return $array;
		}
		if($num[0]['b'] === $num[1]['b'] && $num[3]['b'] === $num[4]['b']){
			$array = array($num[1]['a'] , array($num[1]['b'] , $num[3]['b']),3);
			return $array;
		}
		if($num[1]['b'] === $num[2]['b'] && $num[3]['b'] === $num[4]['b']){
			$array = array($num[1]['a'] , array($num[1]['b'] , $num[3]['b']),3);
			return $array;
		}
		//单对

		for($i = 0 ; $i < $count_num ; $i++){
			for($j = $i+1 ; $j < $count_num ; $j++){
				if($num[$i]['b'] === $num[$j]['b']){
					$array = array($num[$i]['a'] , $num[$i]['b'] , 2);
					return $array;
				}
			}	
		}
		//散牌
		if($num[0]['b'] != $num[1]['b']){
			$array = array($num[0]['a'] , $num[0]['b'] , 1);
			return $array;
		}
		
	}
	
	/**
	 * 牌的数字转换为 A 2 3 4 .... 10 J Q K
	 * @param <int|array> $num
	 */
	public static function num2card($num) {
		$map = array(
			
			11 => 'J',
			12 => 'Q',
			13 => 'K',
			14 => 'A',
		);

		if(!is_array($num)) {
			$n = $num;
			return isset($map[$n]) ? $map[$n] : $n;
		}

		//print_r($return_cards);
		
		
		foreach($num as $k => $n) {
			$num[$k]['a']=$num[$k]['a'];
			$num[$k]['b']=isset($map[$n['b']]) ? $map[$n['b']] : $n['b'];
		}
		//print_r($num);
		return $num;
	}
	/**
	 * 将牌写入数据库
	 * @param $uin
	 * @param $id
	 * @param $guest_uin
	 * @param $turn
	 * @param $owner_cards
	 * @param $stake
	 */
	public static function getCordState($uin , &$id , $guest_uin , $turn , $owner_cards ,  $stake) {
		$Db = loadDB();
		
		// 按格式打包，暂时是用 serialize
		//$state[] =$owner_cards;
		$state['owner_cards'] = $owner_cards;

		$state = serialize($state);
		//print_r($state);
		$time = time();
		
		//把数据写入数组
		$query_data = array(
			'guest_uin' => $guest_uin,
			'turn' => $turn,
			'state' => $state,
			'stake' => $stake,
			'time' => $time,
		);
		
			// 新增
			
			$id = $time;		// 分配一个id
			$query_data['id'] = $id;
			$query_data['uin'] = $uin;
			
			$success = $Db->doInsert('app_showhand_state' , $query_data);
		
		
		return $success ? 0 : 541;
	}
	/**
	 * 要求换牌
	 */
	public static function getAddcards($uin , $id , $cards){
		$Db=loadDB();
		$state['owner_cards'] = $cards;
		$state = serialize($state);
		
		$update_data = array(
			'state' => $state,
		);
		
		$success = $Db->doUpdate('app_showhand_state' , $update_data , "`id` = {$id} AND `uin` = {$uin}");

		return $success ? 0 : 540;
	}
	/**
	 * 获取牌局
	 * @param <int> $uin
	 * @param <int> $id
	 */
	public static function getState($uin , $id) {
		$Db = loadDB();
		
		$result = $Db->getOne("SELECT * FROM `app_showhand_state` WHERE `uin` = {$uin} AND `id` = {$id}");
		if(empty($result)) return array();
	//	print_r($result);
		$result['state'] = unserialize($result['state']);
		
		return $result;
	}
	/**
	 * 加注
	 * @param <int> $id
	 * @param <int> $stake
	 */
	public static function addstake($uin ,  $id , $turn ,$steke){
		$Db=loadDB();
		
		$update_data = array(
			'turn' => $turn,
			'stake' => $steke,
		);
		
		$success = $Db->doUpdate('app_showhand_state' , $update_data , "`uin` = {$uin} AND `id` = {$id}");
		//print_r($success);
		return $success ? 0 : 543;
	}
	/**
	 * 获取个人的梭哈牌局数量
	 */
	public static function getOneCount($uin){
		$Db = loadDB();
		$result= $Db->getOne("SELECT COUNT(*) AS `count` FROM `app_showhand_state` WHERE `uin` = {$uin}");
		return $result;
	}
	/**
	 * 获取个人的梭哈牌局
	 */
	public static function getOneList($uin , $pageSize , $pageCount){
		$Db = loadDB();
		
		$pageSize=($pageSize-1)*$pageCount;
		$list = $Db->getAll("SELECT  * FROM `app_showhand_state` WHERE `uin`={$uin} ORDER BY id DESC LIMIT {$pageSize} , {$pageCount}");
		
		//解析回复
		foreach($list as $k => $row) {
			$list[$k]['state'] = empty($row['state']) ? array() : unserialize($row['state']);
		}
		
		return $list;
	}
	/**
	 * 修改梭哈牌局随机出牌
	 */
	public static function getUpShowhand($id , $owner_uin , $guest_cards , $org_showhand = false){
		$Db=loadDB();
		// 取原数据
		if(empty($org_showhand)) {
			$showhand = self::getState($owner_uin , $id);
			if(empty($showhand)) {
				return 544;	// 该梭哈信息不存在
			}
		} else {
			$showhand = $org_showhand;
		}
		
		$showhand['state']['guest_cards'] = $guest_cards;
		//print_r($showhand['state']);
		// 写入数据
		$update_data = array(
			'state' => serialize($showhand['state']),
		);
		//print_r($)
		$success = $Db->doUpdate('app_showhand_state' , $update_data , '`uin`= '.$owner_uin.' AND `id`= ' . $id);
	//print_r($mora);
		return $success ? 0 : 545;
	}
	/**
	 * 判断对战，1为A胜，2为B胜
	 * @param $fairly
	 */
	public static function getEndFairly($fairly){
		//同花顺(9)＞四条(8)＞富尔豪斯(7)＞同花(6)＞顺子(5)＞三条(4)＞二对(3)＞单对(2)＞散牌(1)
		//$fairly=array(array(3,array(5,3),7),array(2,array(6,2),7));
		
		//判断牌型
		if($fairly[0][2] > $fairly[1][2]){
			return 1;
		}else{
			//当牌型相同
			if($fairly[0][2] === $fairly[1][2]){
				//判断牌大小
				if($fairly[0][1] > $fairly[1][1]){
					return 1;
				}else{
					//当牌为富尔豪斯、二对时
					if(!empty($fairly[0][1][1])){
						//print_r($fairly[1][1][0]);
						//判断富尔豪斯、二对牌的大小
						if($fairly[0][1][0] > $fairly[1][1][0]){
							return 1;
						}else{
							//当牌相等
							if($fairly[0][1][0] === $fairly[1][1][0]){
								//两对中第二对大小
								if($fairly[0][1][1] > $fairly[1][1][1]){
									return 1;
								}else{
									//两对牌相同
									if($fairly[0][1][1] === $fairly[1][1][1]){
										//判断最大那对的花色
										if($fairly[0][0] > $fairly[1][0]){
											return 1;
										}else{
											return 2;
										}
									}else{
										return 2;
									}
								}
							}else{
								return 2;
							}
						}
						
					}
					//当牌相同
					if($fairly[0][1] === $fairly[1][1]){
						//判断花色
						if($fairly[0][0] > $fairly[1][0]){
							return 1;
						}else{
							return 2;
						}
					}else{
						return 2;
					}
				}
			}else{
				return 2;
			}
		}
	}
	/**
	 * 修改梭哈进入牌场uin
	 */
	public static function getUpState($id , $owner_uin , $uin){
		$Db=loadDB();
		$update_data = array(
			'guest_uin' => $uin,
		);
		$success = $Db->doUpdate('app_showhand_state' , $update_data , '`uin`= '.$owner_uin.' AND `id` = ' . $id);
	}
	/**
	 * 修改梭哈个人战绩
	 * @param <int> $id
	 * @param <int> $steke
	 */
	public static function getAlterGain($uin,$gain,$total,$win,$rate){
		$Db=loadDB();
		$time = time();
		$update_data = array(
			'gain' => $gain,
			'total' => $total,
			'win' => $win,
			'rate' => $rate,
			'time' => $time,
		);
		$success = $Db->doUpdate('app_showhand_gain' , $update_data , '`uin` = ' . $uin);
		return $success ? 0 : 546;
	}
	/**
	 * 删除梭哈牌局
	 * @param <int> $uin
	 * @param <int> $id
	 */
	public static function delState($uin , $id) {
		$Db = loadDB();
		
		$success = $Db->query("DELETE FROM `app_showhand_state` WHERE `uin` = {$uin} AND `id` = {$id}");
		
		return $success ? 0 : 525;
	}
	/**
	 * 修改state状态内容
	 * @param <int> $uin
	 * @param <int> $id
	 */
	public static function getAlterStateShowhand($id , $owner_uin , $state){
		$Db=loadDB();
		
		$is_state = serialize($state);
		$update_data = array(
			'state' => $is_state,
		);
		$success = $Db->doUpdate('app_showhand_state' , $update_data , '`uin`= '.$owner_uin.' AND `id`= ' . $id);

		return $success ? 0 : 550;
	}
	/**
	 * 修改游戏设置
	 */
	public static function getAlterShowhand($uin , $set_num){
		$Db=loadDB();
		
		$update_data = array(
			'setshowhand' => $set_num,
		);
		$success = $Db->doUpdate('app_showhand_gain' , $update_data , '`uin`= '.$uin);
		return $success ? 0 : 550;
	}
}
# end of script